# -*- coding: utf-8 -*-
import dlib
import face_recognition
import cv2
import os

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
detector=dlib.get_frontal_face_detector()
camera = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_DUPLEX
face_names = []
face_codings = []
face_num = 0
# person_list = os.listdir("faces/")
# for i in range(len(person_list)):
#     person_name = os.listdir("faces/" + "person_" + str(i + 1))
#     # print(person_name[0])
#     face_img = face_recognition.load_image_file("faces/" + "person_" + str(i + 1) + "/" + person_name[0])
#     face_codings.append(face_recognition.face_encodings(face_img)[0])
#     face_names.append(person_name[0][:person_name[0].index(".")])
while True:
    success,img=camera.read()
    img_new = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)
    process_this_frame = True
    if process_this_frame:
        # marks = face_recognition.face_locations(img_new)
        # codings = face_recognition.face_encodings(img_new, marks)
         # 图片转换成灰色（去除色彩干扰，让图片识别更准确）
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        # rects = detector(gray, 0)  # 人脸检测
        # for rect in rects:
        #     x1, y1, x2, y2 = rect.left(), rect.top(), rect.right(), rect.bottom()
        #     # 在图像上绘制矩形框
        #     cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

        # marks = face_recognition.face_locations(img_new)
        # codings = face_recognition.face_encodings(img_new, marks)

        # for coding in codings:
        #     result = face_recognition.compare_faces(face_codings, coding,0.4)
        #     print(result)
        #     for i in range(len(result)):
        #         if result[i]:
        #             name = face_names[i]
        #             break
        #         if i == len(result)-1:
        #             name = "unknown"
        #         #break
        # process_this_frame = not process_this_frame
        # for (top, right, bottom, left)in (marks):
        #     top *= 4
        #     right *= 4
        #     bottom *= 4
        #     left *= 4
        #     cv2.rectangle(img, (left, top), (right, bottom), (255, 0, 0), 2)
        #     # cv2.putText(img, name, (left + 6, bottom - 6), font, 1.0, (10, 10, 255), 1)
        # cv2.putText(img, "faces: {}".format(len(marks)), (0, 30),
        #                 cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        # cv2.imshow('face', img)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break


camera.release()
cv2.destroyAllWindows()
